#pragma once

#include "Common.h"

#define PIN_BLUE_SWITCH_LEFT_FACE 11
#define PIN_BLUE_SWITCH_RIGHT_NOTE 10

#define BLUE_SWITH_SPOT 3
#define BLUE_SWITCH_NOTE 1
#define BLUE_SWITCH_FACE 2

class BlueSwitch
{
public:

  inline static void Init() 
  {
    NextCheckTimeInMs = 0;
    pinMode( PIN_BLUE_SWITCH_LEFT_FACE, INPUT );
    pinMode( PIN_BLUE_SWITCH_RIGHT_NOTE, INPUT );    
  }

  inline static void UpdateNextCheckTime() 
  {
    NextCheckTimeInMs = globals::CurrentLoopTimeInMs + 266;
  }

  inline static bool IsCheckTimeReached() 
  {
    return (NextCheckTimeInMs < globals::CurrentLoopTimeInMs);
  }

  inline static void DoUpdateState() 
  {
    byte new_state;

    new_state = ReadBlueSwitch();
    if ( BlueSwitchState != new_state ) {
      IsStateChanged = true;
      BlueSwitchState = new_state;
    }    
  }

  inline static void UpdateState() 
  {
    if ( IsCheckTimeReached() ) {
      DoUpdateState();
      UpdateNextCheckTime(); 
    }
  }

  static unsigned long NextCheckTimeInMs;  
  static byte BlueSwitchState;
  static bool StateSwitchRight;
  static bool IsStateChanged;  

private:

  inline static byte ReadBlueSwitch() 
  {
    return 
      (digitalRead(PIN_BLUE_SWITCH_RIGHT_NOTE) << 1) 
      + digitalRead(PIN_BLUE_SWITCH_LEFT_FACE);
  }

};





